package com.fsi.server;

import java.util.List;

import javax.jdo.PersistenceManager;

import com.fsi.client.Customer;
import com.google.gwt.user.client.Window;

public class CustomerJdoDAO implements CustomerDAO {

	@SuppressWarnings("unchecked")
	public List<Customer> listCustomers() {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		List<Customer> result = null;
		try {
			String query = "select from " + Customer.class.getName();
			// + " where loginEmailAddress != 'hhtest@example.com' "// +
			// Query query = pm.newQuery(Customer.class); //
			// " order by userName";
			// query.setFilter("loginEmailAddress == le");
			// query.declareParameters("String le");
			result = (List<Customer>) pm.newQuery(query).execute();
			// result = (List<Customer>) query.execute("test@example.com");
			result.size();
		} finally {
			pm.close();
		}
		return result;
	}

	public void addCustomer(Customer customer) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(customer);
		} finally {
			pm.close();
		}
	}

	public void deleteCustomer(Customer customer) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.currentTransaction().begin();

			// We don't have a reference to the selected Product.
			// So we have to look it up first,
			customer = pm.getObjectById(Customer.class, customer
					.getLoginEmailAddress());
			pm.deletePersistent(customer);

			pm.currentTransaction().commit();
		} catch (Exception ex) {
			pm.currentTransaction().rollback();
			throw new RuntimeException(ex);
		} finally {
			pm.close();
		}
	}

	@Override
	public Customer getCustomerByEmail(String loginEmailAddress) {
		PersistenceManager pm = PMF.get().getPersistenceManager();

		Customer result = null;
		try {
			result = pm.getObjectById(Customer.class, loginEmailAddress);

		} finally {
			pm.close();
		}
		return result;
	}

	@Override
	public Customer updateCustomerDetails(Customer customer, int updateType) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Customer fetchedCustomer = null;
		try {
			try {
				fetchedCustomer = pm.getObjectById(Customer.class, customer
						.getLoginEmailAddress());

				switch (updateType) {
				case Customer.UPDATEFIXED:
					fetchedCustomer.setUserName(customer.getUserName());
					fetchedCustomer.setEmailAddress(customer.getEmailAddress());
					fetchedCustomer.setGender(customer.getGender());
					fetchedCustomer.setCountry(customer.getCountry());
					fetchedCustomer.setPostCode(customer.getPostCode());
					fetchedCustomer.setBirthdate(customer.getBirthdate());
					break;
				case Customer.UPDATEGENERAL:
					fetchedCustomer.setSmoker(customer.isSmoker());
					fetchedCustomer.setDescription(customer.getDescription());
					fetchedCustomer.setFirstDate(customer.getFirstDate());
					break;
				}
				fetchedCustomer.setPostCode(customer.getPostCode());
			} catch (Exception e) {
				Window.alert("Error finding customer: " + e.getMessage());
			}

		} finally {
			pm.close();
		}
		return fetchedCustomer;
	}

}
